package org.zhongyin.Data;


import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jxl.read.biff.BiffException;

import org.zhongyin.Dao.ARCIVE_DBDaoImpl;
import org.zhongyin.Dao.LocalDBDaoImpl;
import org.zhongyin.JPA.Entity.PermanentHistoryData;


/**
 * 
 * @author ZhongYin Zhang
 *
 */
public class NoaaDataWeekend implements INoaaDataWeek{
	/**Historical Noaa Data*/
	private IData histNoaaData;
	/**Forecast Data*/
	private IData forecastNoaaData;
	
	
	public NoaaDataWeekend() throws BiffException, IOException {
		this.histNoaaData = new NoaaData();
		this.forecastNoaaData = new NoaaData();
	}
	
	
	/**
	 * @param histNoaaData the histNoaaData to set
	 * @throws ParseException 
	 */
	public void setHistNoaaData() throws ParseException {	
		ARCIVE_DBDaoImpl dao = new ARCIVE_DBDaoImpl();
		LocalDBDaoImpl ldao = new LocalDBDaoImpl();
		List<Object[]> hData =dao.getHistData();
		List<Object[]> rData =dao.getRecentData();
		rData.addAll(hData);
		
		List<Object[]> filters = new ArrayList<Object[]>();
		Set<Timestamp> datefilter = new HashSet<Timestamp>();
		for(Object[] el: rData){
			if(!datefilter.contains(el[0])){
				datefilter.add((Timestamp) el[0]);
			}else{
				filters.add(el);
			}
		}
		rData.removeAll(filters);
		List<Object[]> today =dao.getTodayData();
		List<PermanentHistoryData> clData = dao.getPermanentHistCLData();
		List<Object[]> combine = dao.combineData(today, rData, clData);
		List<Map<Timestamp, Float>> T_tandRH_t =ldao.getHistMissData();
		combine = dao.insertMissData(combine, clData, T_tandRH_t);
		dao.resetMaxMin_t(combine);
		
		histNoaaData.read(combine);
		
	}
	/**
	 * 
	 * @return return the HistnooaData from Database 
	 * @throws ParseException 
	 */
	public IData getHistNoaaData(){
		return this.histNoaaData;
		
	}
	
	public void setForcastNoaaData() throws ParseException {
		ARCIVE_DBDaoImpl dao = new ARCIVE_DBDaoImpl();
		LocalDBDaoImpl ldao = new LocalDBDaoImpl();
		List<Object[]> lastWeekData = dao.getLastWeekData();//query last week data
		List<Object[]> today =dao.getTodayData();//query today data
		List<PermanentHistoryData> clData = dao.getPermanentHistCLData();//query reported cooling load data
		List<Object[]> combine = dao.combineData(today, lastWeekData, clData);//combine three data above
		List<Map<Timestamp, Float>> T_tandRH_t = ldao.getLastWeekMissData();// query the missing data for last week to prevent t-168 getting null pointer exception
		dao.insertMissData(combine, clData, T_tandRH_t);// insert the missing data to original combine data
		
		List<Object[]> next24hours = dao.getForcastNext24Hours();
		List<Object[]> combine24 = dao.combineNext24HoursData(next24hours, combine);
		
		dao.resetMaxMin_t(combine24);	// Reset the Max_t and Min_t
	 
		forecastNoaaData.read(combine24);
	}
	
	public IData getForcastNoaaData(){
		return this.forecastNoaaData;
		
	}
	
	
}
